﻿using CommunityToolkit.Mvvm.DependencyInjection;
using Kang.Monitor.Wpf.Service;
using Kang.Monitor.Wpf.ViewModels;
using Kang.Monitor.Wpf.Views;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;

namespace Kang.Monitor.Wpf
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private bool _initialized = false;

        public App()
        {
            InitializeComponent();

            if (!_initialized)
            {
                _initialized = true;

                Ioc.Default.ConfigureServices(
                   new ServiceCollection()
                   //Services
                   .AddSingleton<ILoginService, LoginService>()

                   .AddSingleton<ModbusHelp>()

                   //ViewModels
                   .AddTransient<LoginViewModel>()
                   .AddTransient<MainViewModel>()
                   .AddTransient<IndexViewModel>()
                   .AddTransient<ReportViewModel>()
                   .AddTransient<SerialViewModel>()
                   .AddTransient<DensityViewModel>()

                   .BuildServiceProvider());

                _initialized = false;
            }
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            //new MainWindow().ShowDialog();
            if (new LoginView().ShowDialog() == true)
            {
                new MainWindow().ShowDialog();
            };
            Application.Current.Shutdown();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
        }
    }
}